

class MainCategoryController {
    
    def index = { redirect(action:list,params:params) }

    // the delete, save and update actions only accept POST requests
    static allowedMethods = [delete:'POST', save:'POST', update:'POST']

    def list = {
        params.max = Math.min( params.max ? params.max.toInteger() : 10,  100)
        params.sort = params.sort ? params.sort : "sequence" 
    	params.order = params.order ? params.order : "ASC"
        [ mainCategoryInstanceList: MainCategory.list( params ), mainCategoryInstanceTotal: MainCategory.count() ]
    }

    def show = {
        def mainCategoryInstance = MainCategory.get( params.id )

        if(!mainCategoryInstance) {
            flash.message = "MainCategory not found with id ${params.id}"
            redirect(action:list)
        }
        else { return [ mainCategoryInstance : mainCategoryInstance ] }
    }

    def delete = {
        def mainCategoryInstance = MainCategory.get( params.id )
        if(mainCategoryInstance) {
            try {
                mainCategoryInstance.delete()
                flash.message = "MainCategory ${params.id} deleted"
                redirect(action:list)
            }
            catch(org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "MainCategory ${params.id} could not be deleted"
                redirect(action:show,id:params.id)
            }
        }
        else {
            flash.message = "MainCategory not found with id ${params.id}"
            redirect(action:list)
        }
    }

    def edit = {
        def mainCategoryInstance = MainCategory.get( params.id )

        if(!mainCategoryInstance) {
            flash.message = "MainCategory not found with id ${params.id}"
            redirect(action:list)
        }
        else {
            return [ mainCategoryInstance : mainCategoryInstance ]
        }
    }

    def update = {
        def mainCategoryInstance = MainCategory.get( params.id )
        if(mainCategoryInstance) {
            if(params.version) {
                def version = params.version.toLong()
                if(mainCategoryInstance.version > version) {
                    
                    mainCategoryInstance.errors.rejectValue("version", "mainCategory.optimistic.locking.failure", "Another user has updated this MainCategory while you were editing.")
                    render(view:'edit',model:[mainCategoryInstance:mainCategoryInstance])
                    return
                }
            }
            mainCategoryInstance.properties = params
            if(!mainCategoryInstance.hasErrors() && mainCategoryInstance.save()) {
                flash.message = "MainCategory ${params.id} updated"
                redirect(action:show,id:mainCategoryInstance.id)
            }
            else {
                render(view:'edit',model:[mainCategoryInstance:mainCategoryInstance])
            }
        }
        else {
            flash.message = "MainCategory not found with id ${params.id}"
            redirect(action:edit,id:params.id)
        }
    }

    def create = {
        def mainCategoryInstance = new MainCategory()
        mainCategoryInstance.properties = params
        return ['mainCategoryInstance':mainCategoryInstance]
    }

    def save = {
        def mainCategoryInstance = new MainCategory(params)
        if(!mainCategoryInstance.hasErrors() && mainCategoryInstance.save()) {
            flash.message = "MainCategory ${mainCategoryInstance.id} created"
            redirect(action:show,id:mainCategoryInstance.id)
        }
        else {
            render(view:'create',model:[mainCategoryInstance:mainCategoryInstance])
        }
    }
}
